package ua.kharkov.gritsay;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

@SuppressWarnings("deprecation")
public class HttpClientExample {
	
	private final String USER_AGENT = "Mozilla/5.0";

	public static void main(String[] args) throws Exception {
		
		HttpClientExample http = new HttpClientExample();
		
		System.out.println("\nTesting 2 - Send Http POST request");
		
		http.sendPost();
	}

	// HTTP POST request
	private void sendPost() throws Exception {
		
		String url = "http://webfree.in.ua/klishe/index.php?route=information/contact";
		
		@SuppressWarnings("deprecation")
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		
		// add header
		post.setHeader("User-Agent", USER_AGENT);
		
		List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
		
		urlParameters.add(new BasicNameValuePair("email", "null@null.nu"));
		urlParameters.add(new BasicNameValuePair("enquiry", "dssafdsfdsfsdfdsfwer34rwer er werewrewrwerewrwer"));
		urlParameters.add(new BasicNameValuePair("name", "Yuriy"));
		
		post.setEntity(new UrlEncodedFormEntity(urlParameters));
		
		HttpResponse response = client.execute(post);
		
		System.out.println("\nSending 'POST' request to URL : " + url);
		System.out.println("Post parameters : " + post.getEntity());
		System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
		
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		
		StringBuffer result = new StringBuffer();
		String line = "";
		
		while ((line = rd.readLine()) != null) {
			result.append(line);
		}
		
		System.out.println(result.toString());
	}
}
